package org.redis.cluster.registry.zookeeper.test;

import org.apache.commons.lang3.ArrayUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.redis.cluster.RedisCluster;
import org.redis.cluster.config.DefaultConfigManager;
import org.redis.cluster.config.ConfigManager;
import org.redis.cluster.config.RegistryConfig;
import org.redis.cluster.config.ServerConfig;
import org.redis.cluster.config.SystemConfig;
import org.redis.cluster.registry.Node;
import org.redis.cluster.registry.zookeeper.ZookeeperRegistry;
import org.redis.cluster.test.TestRedisCluster;

public class ZookeeperRegistryTester_1 {
	
	private ZookeeperRegistry registry;
	
	@Before
	public void before() {
		this.registry=initRegistry();
	}
	
	protected ZookeeperRegistry initRegistry() {
		ConfigManager configManager=new DefaultConfigManager(null, null, new SystemConfig(null));
		RedisCluster redisCluster=new TestRedisCluster(configManager);
		
		RegistryConfig registryConfig=new RegistryConfig();
		registryConfig.setHost("127.0.0.1:2181");
		registryConfig.setPort(2181);
		registryConfig.setTimeout(1000*3);
		registryConfig.setRoot("/redis-cluster");
		ZookeeperRegistry registry=new ZookeeperRegistry(registryConfig);
		registry.init(redisCluster);
		
		return registry;
	}
	
	@Test
	public void test() throws Exception {
		Node node=new Node(ArrayUtils.EMPTY_BYTE_ARRAY);
		ServerConfig serverConfig=new ServerConfig();
		serverConfig.setServer("test");
		serverConfig.setHost("test");
		serverConfig.setPort(6379);
		serverConfig.setThread(1);
		serverConfig.setBufferSize(1024);
		serverConfig.setTimeout(1000*3);
		serverConfig.setConnection(100);
		
		Assert.assertEquals(true, registry.registerServer(serverConfig));
		Assert.assertNotEquals(0, registry.getServers().size());
		
		System.out.println(registry.getServers());
		
		Assert.assertEquals(true,registry.register("/test", node));
		Assert.assertNotNull(registry.getNode("/test"));
		Assert.assertEquals(true, registry.unregister("/test"));
		
		registry.listen("/test", (String path)->{
			System.out.println(path+"|listen");
		});
		
		System.in.read();
	}
}